home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tobstack.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  2KB  |  81 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  a little test of Obstacks
  5.  Thu Feb 18 11:16:28 1988  Doug Lea  (dl at rocky.oswego.edu)
  6. */
  7.  
  8. #include <assert.h>
  9.  
  10. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  11.                        else _assert(#ex, __FILE__,__LINE__); }
  12.  
  13. #include <stream.h>
  14. #include <Obstack.h>
  15. #include <stddef.h>
  16. #include <ctype.h>
  17.  
  18. int
  19. main()
  20. {
  21.   char*   s[10000];
  22.   int     n = 0;
  23.   int     got_one = 0;
  24.   Obstack os;
  25.   char    c;
  26.  
  27.   s[n++] = (char *)os.copy("\nunique words:");
  28.   assert(os.OK());
  29.   assert(os.contains(s[0]));
  30.  
  31.   cout << "enter anything at all, end with an EOF(^D)\n";
  32.  
  33.   while (cin.good() && n < 10000)
  34.   {
  35.     if (cin.get(c) && isalnum(c))
  36.     {
  37.       got_one = 1;
  38.       os.grow(c);
  39.     }
  40.     else if (got_one)
  41.     {
  42.       char* current = (char *)os.finish(0);
  43.       for (int i = 0; i < n; ++i) // stupid, but this is only a test.
  44.       {
  45.         if (strcmp(s[i], current) == 0)
  46.         {
  47.           os.free(current);
  48.           current = 0;
  49.           break;
  50.         }
  51.       }
  52.       if (current != 0)
  53.         s[n++] = current;
  54.       got_one = 0;
  55.     }
  56.   }
  57.   assert(os.OK());
  58.  
  59.   cout << s[0] << "\n";
  60.  
  61.   for (int i = n - 1; i > 0; -- i)
  62.   {
  63.     assert(os.contains(s[i]));
  64.     cout << s[i] << "\n";
  65.     os.free(s[i]);
  66.   }
  67.  
  68.   assert(os.OK());
  69.   assert(os.contains(s[0]));
  70.  
  71.   cout << "\n\nObstack vars:\n";
  72.   cout << "alignment_mask = " << os.alignment_mask() << "\n";
  73.   cout << "chunk_size = " << os.chunk_size() << "\n";
  74.   cout << "size = " << os.size() << "\n";
  75.   cout << "room = " << os.room() << "\n";
  76.  
  77.   cout << "\nend of test\n";
  78.  
  79.   return 0;
  80. }
  81.